package com.easy.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easy.common.bean.SysMenu;
import com.easy.common.service.IMenuService;
import com.easy.common.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("menu")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @PostMapping("/")
    public ResultData insert(@RequestBody SysMenu sysMenu) {
        SysMenu data = menuService.insert(sysMenu);
        return ResultData.success(data);
    }

    @PutMapping("/")
    public ResultData update(@RequestBody SysMenu sysMenu) {
        SysMenu data = menuService.update(sysMenu);
        return ResultData.success(data);
    }

    @DeleteMapping("/{id}")
    public ResultData delete(@PathVariable Integer id) {
        int result = menuService.delete(id);
        if (result >= 1) {
            return ResultData.success("200", "删除成功", null);
        } else {
            return ResultData.fail();
        }
    }

    @GetMapping("/{id}")
    public ResultData getById(@PathVariable Integer id) {
        SysMenu data = menuService.getById(id);
        return ResultData.success(data);
    }

    @GetMapping("/page")
    public ResultData getListByPage(Page page, SysMenu sysMenu) {
        IPage<SysMenu> data = menuService.getListByPage(page, sysMenu);
        return ResultData.success(data);
    }

    @GetMapping("/list")
    public ResultData getList(SysMenu sysMenu) {
        List<SysMenu> data = menuService.getList(sysMenu);
        return ResultData.success(data);
    }

    @PutMapping("/visible/{id}")
    public ResultData setVisible(@PathVariable Integer id) {
        int result = menuService.setVisible(id);
        if (result >= 1) {
            return ResultData.success("200", "可见性设置成功", null);
        } else {
            return ResultData.fail();
        }
    }
}